package rozetka.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.CacheLookup;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import rozetka.pages.menu.*;
import rozetka.utils.ReportWriter;


/**
 * Created by foneox on 31.03.2014.
 */
public class BasePage {

    public static WebDriver driver;
    private static final By signoutLocator = By.name("signout");
    private static final By searchBoxLocator = By.name("text");
    private static final By searchButtonLocator = By.cssSelector(".btn-link-i");
    private static final By topMenuItemsLocator = By.cssSelector(".m-main-title");

    //main menu elements
    public By computersNotebooksLocator = By.xpath(".//*[@id='computers-notebooks']/div/a");
    public By phonesMp3GpsLocator = By.xpath(".//*[@id='phones-mp3-gps']/div/a");
    public By tvPhotoVideoLocator = By.xpath(".//*[@id='tv-photo-video']/div/a");

    public By householdAppliancesLocator = By.xpath(".//*[@id='bt']/div/a");
    public By outdoorsmanLocator = By.xpath(".//*[@id='outdoorsman']/div/a");
    public By kidsLocator = By.xpath(".//*[@id='kids']/div/a");

    public By houseGardenLocator = By.xpath(".//*[@id='house_garden']/div/a");
    public By autoGoodsLocator = By.xpath(".//*[@id='273290']/div/a");
    public By airLandTicketsLocator = By.xpath(".//*[@id='fatmenu_14']/div/a");

    private By signInLinkLocator  = By.name("signin");
    private By logoutLingLocator = By.name("signout");
    private By loginLogoutLinkLocator = By.cssSelector(".m-user-link.xhr");


    public BasePage(WebDriver driver) {
        this.driver = driver;
    }

    public Boolean isUserLoggedIn() {
        ReportWriter.info("Waiting for login-logout link to appear...");

        try {
            Thread.sleep(5000);
        } catch (Exception ex) {
            ReportWriter.error("Exception on Thread.sleep()");
        }

        WebElement loginLogoutLink = driver.findElement(loginLogoutLinkLocator);
        ReportWriter.info("State of the loginLogout page: "+loginLogoutLink.getAttribute("name"));
        if (loginLogoutLink.getAttribute("name").equals("signout")){
            return true;
        }else if(loginLogoutLink.getAttribute("name").equals("signin")){
            return false;
        }else{
            Assert.fail(ReportWriter.error("isUserLoggedIn() method failed"));
            return null;
        }
    }

    public SearchResultsPage search(String searchTerm) {
        //fixme: does not take redirection into account
        WebElement searchBox = driver.findElement(searchBoxLocator);
        searchBox.clear();
        searchBox.sendKeys(searchTerm);
        driver.findElement(searchButtonLocator).click();
        return new SearchResultsPage(driver);

    }

    public boolean isSearchQueryPresentInSearchBox(String searchTerm) {
        WebElement searchBox = driver.findElement(searchBoxLocator);
        ReportWriter.info("Checking if searchquery is present in the searchbox");
        return searchBox.getAttribute("value").equals(searchTerm);
    }

    public ComputersNotebooksPage openComputersNotebooksPage(){
        ReportWriter.info("Opening of Computers and Notebooks page");
        driver.findElement(computersNotebooksLocator).click();
        return new ComputersNotebooksPage(driver);
    }
    public String getCurrentUrl(){
        return driver.getCurrentUrl();
    }

    public PhonesMp3GpsPage openPhonesMp3GpsPage() {
        ReportWriter.info("Opening of PhonesMp3Gps Page");
        driver.findElement(phonesMp3GpsLocator).click();
        return new PhonesMp3GpsPage(driver);
    }

    public TvPhotoVideoPage openTvPhotoVideoPage() {
        ReportWriter.info("Opening of TvPhotoVideo Page");
        driver.findElement(tvPhotoVideoLocator).click();
        return new TvPhotoVideoPage(driver);
    }

    public HouseholdAppliancesPage openHouseholdAppliancesPage() {
        ReportWriter.info("Opening of HouseholdAppliances Page");
        driver.findElement(householdAppliancesLocator).click();
        return new HouseholdAppliancesPage(driver);
    }

    public OutdoorsmanPage openOutdoorsmanPage() {
        ReportWriter.info("Opening of Outdoorsman Page");
        driver.findElement(outdoorsmanLocator).click();
        return new OutdoorsmanPage(driver);
    }

    public KidsMenuPage openKidsMenuPage() {
        ReportWriter.info("Opening of Kids Page");
        driver.findElement(kidsLocator).click();
        return new KidsMenuPage(driver);
    }

    public HouseGardenPage openHouseGardenMenuItem() {
        ReportWriter.info("Opening of HouseGarden Page");
        driver.findElement(houseGardenLocator).click();
        return new HouseGardenPage(driver);
    }

    public AutoGoodsMenuPage openAutoGoodsMenuPage() {
        ReportWriter.info("Opening of AutoGoods Page");
        driver.findElement(autoGoodsLocator).click();
        return new AutoGoodsMenuPage(driver);
    }

    public AirLandTickets openLandTicketsPage() {
        ReportWriter.info("Opening of LandTickets Page");
        driver.findElement(airLandTicketsLocator).click();
        return new AirLandTickets(driver);
    }

    public LoginDialog followLoginDialog() {
        driver.findElement(signInLinkLocator).click();
        return new LoginDialog(driver);
    }

    public void logout() {
        driver.findElement(logoutLingLocator).click();
    }
}
